import { Module } from '@nestjs/common'
import { TestModule } from './test.module'
import { AuthModule } from './auth.module'
import { TypeOrmModule } from '@nestjs/typeorm'
import { join } from 'path'
import { app } from 'electron'
import SnakeCaseNamingStrategy from '../db/NamingStrategy'
import entities from '../entity'
import driver from 'better-sqlite3-multiple-ciphers'
import { UserModule } from './user.module'
import logger from '../../core/logger'
import { is } from '@electron-toolkit/utils'
const globalConfig = import.meta.env

const appName = app.getName()
const dbPath = is.dev
  ? join(process.cwd(), 'stores', 'db', `${appName}.db`)
  : join(app.getPath('appData'), appName, 'stores', 'db', `${appName}.db`)

logger.info(`dbPath = ${dbPath}`)
function initDb() {
  logger.info(`dbPath = ${JSON.stringify(globalConfig)}`)
  return TypeOrmModule.forRoot({
    type: 'better-sqlite3',
    database: dbPath,
    autoLoadEntities: true,
    synchronize: true,
    entities: [...entities],
    driver: driver,
    namingStrategy: new SnakeCaseNamingStrategy(),
    extra: {
      cipher: 'aes-256-cbc',
      password: '123456'
    },
    key: '123456'
  })
}
@Module({
  imports: [initDb(), TestModule, AuthModule, UserModule],
  exports: [TestModule, AuthModule, UserModule]
})
export class AppModule {}
